<template>

    <router-view @attentions="attentions" :author="author" @dellove="dellove" @myfavourite="myfavourite" @delfavourite="delfavourite"></router-view>
</template>


<script>
import { getJsonData } from "./api/home.js";
export default {
   data() {
    return {
        author: [],
        mylove:[],
        recommended:""
    };
   },
   methods:{
    // 添加关注
    attentions(index){
        // 传入的是uid
        console.log(index);
        // 判断在我关注的里面是否已经关注
        let id=this.author.findIndex((item)=>item.id==index)

        // ==-1为true就是找不到，就是没有关注
        if(id==-1){
            //查找总数据里面的下标为index2
            let index2=this.recommended.findIndex((item)=>item.id==index)
            //把找到的下标存入author里面
            this.author.unshift(this.recommended[index2])
            //把author数据放入local
            window.localStorage.setItem("authors",JSON.stringify(this.author));
        }


        // console.log(this.author);
    },
    // 取消关注
    dellove(index){
        //获得点击删除的下标
        console.log(index);
        //从local获取关注的author
        this.author=JSON.parse(window.localStorage.getItem("authors"));
        //查找id
        let id=this.author.findIndex((item)=>item.id==index)
        //删除
        this.author.splice(id,1)
        //保存回local
        window.localStorage.setItem("authors",JSON.stringify(this.author));
    },
    //我的点赞
    myfavourite(index){
        // 传入的是uid

        // 判断在我关注的里面是否已经点赞
        let id=this.mylove.findIndex((item)=>item.id==index)
        // ==-1为true就是找不到，就是没有关注
        if(id==-1){
            //查找总数据里面的下标为index2
            let index2=this.recommended.findIndex((item)=>item.id==index)
            //把找到的下标存入mylove里面
            this.mylove.unshift(this.recommended[index2])
            //把mylove数据放入local
            window.localStorage.setItem("myloves",JSON.stringify(this.mylove));

        }
    },
    //取消点赞
    delfavourite(index){
        //获得点击删除的下标
        console.log(index);
        //从local获取关注的mylove
        this.mylove=JSON.parse(window.localStorage.getItem("myloves"));
        //查找id
        let id=this.mylove.findIndex((item)=>item.id==index)
        //删除
        this.mylove.splice(id,1)
        //保存回local
        window.localStorage.setItem("myloves",JSON.stringify(this.mylove));
    }
   },
   created() {

      getJsonData().then((data)=>{
        this.recommended=data.recommended
    })
   }
}
</script>
<style lang="scss">

</style>
